<template>
  <div class="login_container">
    <div class="login_box"></div>
    <div class="login_img">
      <img src="../assets/logo.png" style="width:100%;height:100% " />
    </div>
    <div class="login_form">
      <el-form label-width="80px" :model="loginForm" :rules="rules" ref="form">
        <!-- 用户名 -->
        <el-form-item label="用户名:" prop="username">
          <el-input
            placeholder="用户名"
            prefix-icon="el-icon-user"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="密码:" prop="password">
          <el-input
            placeholder="密码"
            type="password"
            prefix-icon="el-icon-s-cooperation"
            v-model="loginForm.password"
          ></el-input>
        </el-form-item>
        <el-form-item >
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLogin">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>

export default {
  data(){
    return {

      loginForm:{
        usernamne:  '',
        password :''
      },
      rules: {
          username: [
            { required: true, message: '请输入用户', trigger: 'blur' }
           
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
          ]
      }    

    }
  },
  methods:{
    // 重置表单
    resetLogin(){
      this.$refs.form.resetFields();
    },
    login(){
      // 表单预验证
      this.$refs.form.validate(async valid =>{
        if(valid){
          const {data: res} = await this.$http.post("login", this.loginForm)
          console.log(res)
            if(res.meta.status != 200){
                return this.$message.error("登录失败");
            }else{
              this.$message.success("登录成功")
              window.sessionStorage.setItem("token", res.data.token)
              // 路由跳转
              this.$router.push("/home")
            }
          
         
        }
        
      })
    }
  }
};
</script>

<style scoped>
body,
html {
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
}

.login_container {
  background-color: rgb(79, 90, 189);
  height: 100%;
}

.login_box {
  width: 30%;
  height: 40%;
  border: 1px solid black;
  background-color: aliceblue;

  position: absolute;
  left: 50%;
  top: 50%;
  /* 相对于自身宽度和高度 */
  transform: translate(-50%, -50%);
  border-radius: 10px;
  box-shadow: 10px 10px 5px #888888;
}

.login_img {
  width: 10%;
  height: 20%;

  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -150%);
  border-radius: 50%;
}

.login_form {
  position: absolute;
  top: 40%;
  left: 40%;
}
</style>
 





